home *** CD-ROM | disk | FTP | other *** search
/ Precision Software Appli…tions Silver Collection 1 / Precision Software Applications Silver Collection Volume One (PSM) (1993).iso / demos / devel3.exe / COLOR16.C < prev    next >
C/C++ Source or Header  |  1992-05-29  |  4KB  |  149 lines

  1. /* Routine to setup and compute colors */
  2. /* for 16-color mode                */
  3.  
  4. /* Written by Dave Stampe Mar 21 1992 */
  5.  
  6. /* Copyright 1992 by Dave Stampe and Bernie Roehl.
  7.    May be freely used to write software for release into the public domain;
  8.    all commercial endeavours MUST contact Bernie Roehl and Dave Stampe
  9.    for permission to incorporate any part of this software into their
  10.    products!
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <dos.h>
  15. #include "rend386.h"
  16.  
  17.             /* 320x200x16 screen definition */
  18.  
  19. struct Screeninfo screeninfo =
  20.          { 0, 0, 319, 199, 160, 100, 16, 8};
  21.  
  22.              /* colors to use on screen */
  23.  
  24. int screen_clear_color = 15;
  25. int wireframe_color    = 12;
  26. int highlight_color    = 14;
  27. int highest_color      = 15;
  28.  
  29.              /* use linear palette instaed of EGA palette map */
  30.  
  31. static char rst_pal[16] = {0,1,2,3,4,5,20,7,56,57,58,59,60,61,62,63};
  32.  
  33. void read_palette(char *p)   /* save all palette map reg's */
  34. {
  35.     int i;
  36.     union REGS regs;
  37.  
  38.     for (i = 0; i < 16; i++)
  39.         {
  40.         regs.x.ax = 0x1007;
  41.         regs.h.bl = i;
  42.         int86(0x10,®s, ®s);
  43.         p[i] = regs.h.bh;
  44.         }
  45. }
  46.  
  47.  
  48. static void
  49. set_rgb(int i, int r, int g, int b)   /* load DAC slot eq. to palette */
  50. {
  51.     union REGS regs;
  52.  
  53.     regs.x.ax = 0x1010;
  54.     regs.x.bx = rst_pal[i];
  55.     regs.h.dh = r;
  56.     regs.h.ch = g;
  57.     regs.h.cl = b;
  58.     int86(0x10,®s,®s);
  59. }
  60.  
  61.  
  62.  
  63. set_colors()               /* setup desired DAC palette */
  64. {
  65.     int i;
  66.  
  67.     read_palette(rst_pal);
  68.  
  69.     set_rgb(15,10,20,30);        /* screen background */
  70.     for (i = 1; i < 16; i++)
  71.     set_rgb(i-1, i*4, i*3+4, i*2+4);     /* brightnesses */
  72.     return(0);
  73. }
  74.  
  75.  
  76.  
  77. reset_colors()
  78. {
  79.     return 0;
  80. }
  81.  
  82.  
  83.  
  84. extern int poly_cosine(void *p);  /* RETURNS 128*COS(LIGHT ANGLE) */
  85.  
  86. /* USER POLYGON LIGHTING ROUTINE: DETERMINES POLY COLOR # */
  87.  
  88. /* The 16-bit color the user specifies for a polygon is broken down as
  89.    follows:
  90.          H R SS CCCC BBBBBBBB
  91.  
  92.    H is the highlight flag (the polygon should be highlighted in
  93.    some way, usually by outlining it in the highlight_color given above).
  94.  
  95.    R is a reserved bit, which should be set to zero
  96.  
  97.    SS is a two-bit field specifying one of four surface types:
  98.  
  99.       00 is a constant-color surface; the 4-bit field CCCC is ignored, and the
  100.      8-bit field BBBBBBBB is used as an absolute color number
  101.  
  102.       01 is a cosine-lit surface; the 4-bit field CCCC specifies one of 16
  103.      basic colors, and the 8-bit brightness field BBBBBBBB is multiplied
  104.      by the cosine of the angle between the light source and the polygon's
  105.      surface normal to provide a 4-bit shading value.
  106.  
  107.       10 is a pseudo-metallic surface; the CCCC field gives the starting hue,
  108.      and the BBBBBBBB value is ignored.  The color will cycle through
  109.      the different shades to give a 'metallic' effect.
  110.  
  111.       11 is a pseudo-transparent surface made up of alternating rows of
  112.      spaced dots; other than that, it behaves like a pseudo-metallic
  113.      surface.
  114.  
  115.    This routine maps the above into an 8-bit color number in the low byte
  116.    of its return value, and passes through the top four bits.
  117.  
  118.    16-COLOR NOTES:
  119.    The above is the standard input, but mapping to 16-colors is
  120.    difficult.  One method is to support only brightnesses and absolute
  121.    colors (modulo 16).
  122.  
  123.  */
  124.  
  125. int user_poly_color(POLY *p, int pcolor, long maxz)
  126. {
  127.     unsigned hilite = pcolor & 0xF000;        /* highlight flag  (MSB) */
  128.     unsigned bright = pcolor & 0xFF;        /* mask out albedo (7 bits) */
  129.     unsigned hue = (pcolor & 0x0F00) >> 8;    /* basis color */
  130.     unsigned int color;
  131.  
  132.     if(hue==0) return ((bright&15) | hilite);   /* abs. color */
  133.  
  134.     if ((pcolor & 0x3000) == 0)      /* fixed (unlit) color */
  135.      {
  136.       if(bright>14) bright = 14;
  137.       return (hilite | bright);
  138.      }
  139.  
  140.     color = (((poly_cosine(p)+130)>>2) * (bright>>3) * (hue+3)) / 2250;
  141.  
  142.       /*    color = color>>4;*/
  143.     if (color < 0)  return( hilite);
  144.     if (color > 14) return (14 | hilite);
  145.     return (color | hilite);
  146. }
  147.  
  148.  
  149.